﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using ADEVSO.Util;
using SmartParking.Bussiness;
using SmartParking.Data.Entities;
using SmartParking.Web.Models;

namespace SmartParking.Controllers
{
    [Authorize]
    public class PaymentController : BaseController
    {
        private bool IsMobileDevice()
        {
            string returnUrl = Request.QueryString["ReturnUrl"];
            if (Request.Browser.IsMobileDevice || ((returnUrl != null) && returnUrl.StartsWith("/Mobile/", StringComparison.OrdinalIgnoreCase)))
            {
                return true;
            }
            return false;
        }
       
        public ActionResult Index()
        {
            //var type = Request.Url.Segments[Request.Url.Segments.Count() - 1];
            var type = Convert.ToInt32(Request["type"]);
            switch (type)
            {
                // SmartCash
                case 1:
                    {
                        //TODO: Implement SmartCash payment
                        return View();
                    }
                // PayPal
                case 2:
                    {
                        return RedirectToAction("PostToPayPal");
                    }
                default:
                    {
                        return View();
                    }
            }
        }

        public ActionResult PostToPayPal()
        {
            return View("PostToPayPal", PayPalManager.PostToPayPal(HttpContext.ApplicationInstance.Application, Request, Session, User.Identity.Name, Request.UserHostAddress));
        }

        [HttpPost]
        public ActionResult IPN()
        {
            try
            {
                LogManager.Debug(1, Request.UserHostAddress, String.Format("[IPN] Begin"));
                PayPalManager.PayPalResponse response = PayPalManager.GetPayPalIPNResponse(Request, PayPalSettings.Sandbox);
                LogManager.Debug(1, Request.UserHostAddress, String.Format("[IPN] TXN_ID {0} Session {1} Total {2} Fee {3}", response.Info["txn_id"], response.Info["invoice"], response.Info["mc_gross"], response.Info["mc_fee"]));
                LogManager.Debug(1, Request.UserHostAddress, String.Format("[IPN] End"));
            }
            catch (Exception ex)
            {
                LogManager.Debug(1, Request.UserHostAddress, String.Format("[IPN] Error: {0}", ex.Message));
                return View(ex.Message);
            }
            return View();
        }

        [HttpPost]
        public ActionResult NotifyFromPayPal()
        {
            try
            {
                LogManager.Debug(1, Request.UserHostAddress, String.Format("[NotifyFromPayPal] Begin"));
                //TODO: Replace 1 with ManagerID
                PaymentManager.ProcessPayPalPayment(HttpContext.ApplicationInstance.Application, Request, 1, PayPalSettings.Sandbox);
                LogManager.Debug(1, Request.UserHostAddress, String.Format("[NotifyFromPayPal] End"));
            }
            catch (Exception ex)
            {
                LogManager.Debug(1, Request.UserHostAddress, String.Format("[NotifyFromPayPal] Error: {0}", ex.Message));
                return View(ex.Message);
            }
            return View(String.Empty);
        }

        public ActionResult ReturnFromPayPal()
        {
            var sessionID = Request.Url.Segments[Request.Url.Segments.Count() - 1];
            //ReservationsManager.CancelProcessingReservations(HttpContext.ApplicationInstance.Application, sessionID);
            //TODO: Redirect to Confirm page
            return RedirectToAction("Reservations", "Home");
        }

        public ActionResult CancelFromPaypal()
        {
            var sessionID = Request.Url.Segments[Request.Url.Segments.Count() - 1];
            ReservationsManager.CancelProcessingReservations(HttpContext.ApplicationInstance.Application, sessionID);
            return RedirectToAction("Reservations", "Home");
        }

        public ActionResult TestValue(string id)
        {
            var reservations = ReservationsManager.GetProcessingReservations(id);
            return View(reservations.Count().ToString());
        }
    }
}